package com.example.user

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.room.AppDbUtils
import com.example.user.databinding.ActivityLoginBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch
import retrofit2.Retrofit
@Route(path = "/user/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {
    override fun initView() {
        //点击获取验证码，发送验证码
        binding.btnSendcode.setOnClickListener {
            viewModel.sendCode(binding.etPhone.text.toString())
        }
        //点击实现登录
        binding.btnLogin.setOnClickListener {
            viewModel.login(binding.etCode.text.toString(),binding.etPhone.text.toString())
        }
        //点击跳转到密码登录界面
        binding.tvLoginpwd.setOnClickListener {
            //调用两个方法
            ff1()
            ff2()
            ff3()
            //startActivity(Intent(this,LoginPwdActivity::class.java))
            ARouter.getInstance().build("/user/LoginPwdActivity").navigation()
        }
        //富文本
        val builder = SpannableStringBuilder(binding.tvFwb.text.toString())
        //设置文字颜色
        builder.setSpan(
            ForegroundColorSpan(Color.RED),
            7,
            12,
            SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE
        )
        //点击事件
        val clickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
               //跳转到指定的webview界面
              /*  val intent = Intent(this@LoginActivity, WebViewActivity::class.java)
                intent.putExtra("url","http://www.baidu.com")
                startActivity(intent)*/
                ARouter.getInstance().build("/user/WebViewActivity").withString("url","http://www.baidu.com").navigation()
            }
        }
        val clickableSpan2 = object : ClickableSpan() {
            override fun onClick(widget: View) {
                //跳转到指定的webview界面
              /*  val intent = Intent(this@LoginActivity, WebViewActivity::class.java)
                intent.putExtra("url","https://www.bilibili.com/")
                startActivity(intent)*/
                ARouter.getInstance().build("/user/WebViewActivity").withString("url","https://www.bilibili.com/").navigation()
            }
        }
        builder.setSpan(clickableSpan, 7, 13, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        builder.setSpan(clickableSpan2,14,20, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.tvFwb.movementMethod = LinkMovementMethod.getInstance()
        binding.tvFwb.text = builder
    }

    private fun ff3() {
        //高阶函数  标准库函数  内联 扩展函数  反射 注解 委托 枚举类 密封类
        //(Int)->Int let with apply also inline 类名.方法名（）{} user::class  anno by enum sealed
    }

    private fun ff2() {
        //定义一个channel 发送接收数据
        val channel = Channel<Int>()
        //发送数据
        lifecycleScope.launch {
            channel.send(1)
            channel.close()
        }
        //接收数据
        lifecycleScope.launch {
            for (i in channel){
                println("接收的数据$i")
            }
        }
    }

    private fun ff1() {
        //构建集合
        val list = mutableListOf(1,2,3,4)
        lifecycleScope.launch {
            //构建冷流
            val flow = flow<Int> {
                list.forEach {
                    delay(2000)
                    emit(it)
                }
            }.buffer().map { it*it }.filter { it>4 }.flowOn(Dispatchers.IO)//.shareIn(this, SharingStarted.Eagerly)
            //接收并打印
            flow.collect{
                println(it)
            }
        }

    }

    override fun initData() {
        viewModel.sendCodeSuccess.observe(this){
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.sendCodeFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.loginSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)
                //startActivity(Intent(this,UserInfoActivity::class.java))
                ARouter.getInstance().build("/user/UserInfoActivity").navigation()
                AppDbUtils.getAppDb().getUserDao().insert(it.data)
            }
        }
        viewModel.loginFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<UserViewModel> = UserViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_login

}